package jehc.djshi.sys.web;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BasePage;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseSearch;
import jehc.djshi.common.entity.OauthAccountEntity;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.sys.model.XtKnowledge;
import jehc.djshi.sys.service.XtKnowledgeService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 平台知识内容
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtKnowledge")
@Api(value = "平台知识内容API",tags = "平台知识内容API",description = "平台知识内容API")
public class XtKnowledgeController extends BaseAction {

	@Resource
	private XtKnowledgeService xtKnowledgeService;

	/**
	 * 查询平台知识内容列表并分页
	 * @param baseSearch
	 * @throws UnsupportedEncodingException
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping(value="/list")
	@Auth(value = "/xtKnowledge/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询平台知识内容列表并分页", notes="查询平台知识内容列表并分页")
	public BasePage<List<XtKnowledge>> getXtKnowledgeListByCondition(@RequestBody(required=true)BaseSearch baseSearch) throws UnsupportedEncodingException{
		Map<String, Object> condition = new HashMap<String, Object>();
		condition =  baseSearch.convert();
		commonHPager(baseSearch);
		List<XtKnowledge> xtKnowledgeList = xtKnowledgeService.getXtKnowledgeListByCondition(condition);
		for(XtKnowledge xtKnowledge:xtKnowledgeList){
			if(!StringUtil.isEmpty(xtKnowledge.getCreateId())){
				OauthAccountEntity createBy = getAccount(xtKnowledge.getCreateId());
				if(null != createBy){
					xtKnowledge.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(xtKnowledge.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(xtKnowledge.getUpdateId());
				if(null != modifiedBy){
					xtKnowledge.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<XtKnowledge> page = new PageInfo<XtKnowledge>(xtKnowledgeList);
		return outPageStr(page,baseSearch);
	}

	/**
	 * 查询
	 * @param keywords
	 */
	@GetMapping(value="/query")
	@Auth(value = "/xtKnowledge/query",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询平台知识内容列表", notes="查询平台知识内容列表")
	public List<XtKnowledge> query(BaseSearch baseSearch,String keywords){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("title",keywords);
		commonHPager(baseSearch);
		List<XtKnowledge> xtKnowledgeList = xtKnowledgeService.getXtKnowledgeListByCondition(condition);
		return xtKnowledgeList;
	}

	/**
	 * 查询单个平台知识内容
	 * @param id
	 */
	@GetMapping(value="/get/{id}")
	@Auth(value = "/xtKnowledge/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个平台知识内容", notes="查询单个平台知识内容")
	public BaseResult getXtKnowledgeById(@PathVariable("id")String id){
		XtKnowledge xtKnowledge = xtKnowledgeService.getXtKnowledgeById(id);
		if(!StringUtil.isEmpty(xtKnowledge.getCreateId())){
			OauthAccountEntity createBy = getAccount(xtKnowledge.getCreateId());
			if(null != createBy){
				xtKnowledge.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(xtKnowledge.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(xtKnowledge.getUpdateId());
			if(null != modifiedBy){
				xtKnowledge.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(xtKnowledge);
	}

	/**
	 * 添加
	 * @param xtKnowledge
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个平台知识内容", notes="创建单个平台知识内容")
	public BaseResult addXtKnowledge(@RequestBody XtKnowledge xtKnowledge){
		int i = 0;
		if(null != xtKnowledge){
			xtKnowledge.setId(toUUID());
			i=xtKnowledgeService.addXtKnowledge(xtKnowledge);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param xtKnowledge
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个平台知识内容", notes="编辑单个平台知识内容")
	public BaseResult updateXtKnowledge(@RequestBody XtKnowledge xtKnowledge){
		int i = 0;
		if(null != xtKnowledge){
			i=xtKnowledgeService.updateXtKnowledge(xtKnowledge);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除平台知识内容", notes="删除平台知识内容")
	public BaseResult delXtKnowledge(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=xtKnowledgeService.delXtKnowledge(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 知识库数
	 * @return
	 */
	@GetMapping(value="/count")
	@Auth(value = "/xtKnowledge/count",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	@ApiOperation(value="知识库数", notes="知识库数")
	public BaseResult count(){
		int count = xtKnowledgeService.getXtKnowledgeCount(new HashMap<>());
		return outDataStr(count);
	}
}
